package notificador.ip;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;

import jogoBolaoeArmazenador.rn.Usuario;

public class IPNotificacao {

	
	public void addNotificacao (Usuario user, String text) throws ClassNotFoundException, SQLException{
		Class.forName("org.postgresql.Driver");
		Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/senaonline","postgres", "bojador");
		Statement stm = con.createStatement();	
		String query = "INSERT INTO	notificacao (id_usuario, notificacao) VALUES ('"+user.get_id()+"', '" + text + "')";
		stm.executeUpdate(query);
		stm.close();
		con.close();
	}
	
	
	public void removeNotificacoes (Usuario user) throws SQLException, ClassNotFoundException{
		Class.forName("org.postgresql.Driver");
		Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/senaonline","postgres", "bojador");
		Statement stm = con.createStatement();	
		String query = "DELETE FROM notificacao WHERE notificacao.id_usuario = "+user.get_id()+"";
		stm.executeUpdate(query);
		stm.close();
		con.close();
	}
	
	public ArrayList<String> recuperaNotificacoes (Usuario user) throws SQLException, ClassNotFoundException{
		ArrayList<String> set = new ArrayList<String>();
		String mensagem; 
		Class.forName("org.postgresql.Driver");
		Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/senaonline","postgres", "bojador");
		Statement stm = con.createStatement();
		ResultSet rs = stm.executeQuery("SELECT * FROM notificacao WHERE notificacao.id_usuario="+user.get_id()+"");
		while(rs.next()){
			mensagem = rs.getString("notificacao");
			set.add(mensagem);
		}
		rs.close();
		stm.close();
		con.close();
		return set;
	}
}
